# 使用精确的 Node.js 22.18.0 slim 镜像（基于 Debian Bookworm）
FROM node:22.18.0-slim

# 设置单独的目录用于不同用途
# /app - 应用代码
# /repos - Git仓库代码
# /dist - 构建输出
WORKDIR /app

# 替换 APT 源为阿里云（注意：Node 22 基于 Debian Bookworm，不是 Bullseye！）
RUN echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free" > /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/debian/ bookworm main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free" >> /etc/apt/sources.list && \
    echo "deb-src http://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free" >> /etc/apt/sources.list

# 安装 Python 3.11 和 venv
RUN apt-get update && apt-get install -y \
    python3.11 \
    python3-pip \
    python3.11-venv \
    git \
    curl \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 创建并激活虚拟环境
RUN python3.11 -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# 安装 Python 依赖
COPY requirements.txt .
RUN pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple && \
    pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

# 复制应用代码到 /app
COPY . .

# 创建独立的目录用于仓库和构建输出
RUN mkdir -p /repos /dist

# 设置环境变量指向新的目录位置
ENV REPOS_PATH=/repos
ENV DIST_PATH=/dist

# 设置 Flask 环境
ENV FLASK_APP=app.py
EXPOSE 5000

# 启动 Flask 应用
CMD ["python", "-m", "flask", "run", "--host=0.0.0.0"]